package com.xiaol.business.controller;

import com.xiaol.business.model.User;
import com.xiaol.business.service.user.UserService;
import com.xiaol.framework.result.ResultUtil;
import com.xiaol.framework.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public Result<List<User>> addUser(@RequestParam("userName") String userName,
                                      @RequestParam("password") String password,
                                      @RequestParam("phone") String phone){
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        user.setPhone(phone);
        return ResultUtil.success(userService.addUser(user));
    }

    @RequestMapping(value = "/deleteUserById", method = RequestMethod.GET)
    public Result<List<User>> deleteUserById(@RequestParam("userId") Integer userId){
        return ResultUtil.success(userService.deleteUserById(userId));
    }

    @RequestMapping(value = "/updateUserById", method = RequestMethod.GET)
    public Result<List<User>> updateUserById(@RequestParam("userId") Integer userId,
                                             @RequestParam(value="userName", required = false) String userName,
                                             @RequestParam(value="password", required = false) String password,
                                             @RequestParam(value="phone", required = false) String phone){
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setPassword(password);
        user.setPhone(phone);
        return ResultUtil.success(userService.updateUserById(user));
    }

    @RequestMapping(value = "/queryUserById", method = RequestMethod.GET)
    public Result<List<User>> queryUserById(@RequestParam("userId") Integer userId){
        return ResultUtil.success(userService.queryUserById(userId));
    }

    @RequestMapping(value = "/queryUserList", method = RequestMethod.GET)
    public Result<List<User>> queryUserList(){
        return ResultUtil.success(userService.queryUserList());
    }

    @RequestMapping(value = "/queryUserByPage", method = RequestMethod.GET)
    public Result<List<User>> queryUserByPage(@RequestParam("pageNum")Integer pageNum,
                                              @RequestParam("pageSize")Integer pageSize){
        return ResultUtil.success(userService.queryUserByPage(pageNum,pageSize));
    }

    @RequestMapping(value = "/transactionTest", method = RequestMethod.GET)
    public Result transactionTest(){
        return ResultUtil.success(userService.transactionTest());
    }

}
